package com.cc.secondKill.commnons;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStreamReader;
import java.util.Properties;

public class PropertiesUtils {

    private static Properties properties = null;
    private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

    static {
        String fileName = "secondKill.properties";
        properties = new Properties();
        try {
            properties.load(new InputStreamReader(PropertiesUtils.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
        }catch (Exception e){
            logger.error("加载配置文件异常",e);
        }
    }

    public static String getByKey(String key){
        String result = properties.getProperty(key);
        if (StringUtils.isEmpty(result)){
            return null;
        }
        return result;
    }

    public static String getByKeyWithDefaultValue(String key, String defaultValue){
        String result = properties.getProperty(key);
        if (StringUtils.isEmpty(result)){
            return defaultValue;
        }
        return result;
    }
}
